﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using RapChieuPhim.DTO;
using System.Data.SqlClient;
using RapChieuPhim.BUS;

namespace RapChieuPhim.DAO
{
    class SuatChieuDAO
    {
        public SuatChieuDTO GetByID(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaSuatChieu", id);
            DataTable dt = cn.SqlExcuteQuery("LaySuatChieuTheoID", param);
            SuatChieuDTO SuatChieu = new SuatChieuDTO();

            if (dt.Rows.Count != 0)
            {
                SuatChieu.MaSuatChieu = int.Parse(dt.Rows[0]["MaSuatChieu"].ToString());
                SuatChieu.CaChieu = CaChieuBUS.GetByID(int.Parse(dt.Rows[0]["MaCaChieu"].ToString()));
                SuatChieu.Phong = PhongChieuBUS.GetByID(int.Parse(dt.Rows[0]["MaPhong"].ToString()));
                SuatChieu.GioBD = DateTime.Parse(dt.Rows[0]["GioBD"].ToString());
                SuatChieu.GioKT = DateTime.Parse(dt.Rows[0]["GioKT"].ToString());
            }
            return SuatChieu;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllSuatChieu");

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    SuatChieuDTO SuatChieu = new SuatChieuDTO();
                    SuatChieu.MaSuatChieu = int.Parse(dr["MaSuatChieu"].ToString());
                    SuatChieu.CaChieu = CaChieuBUS.GetByID(int.Parse(dr["MaCaChieu"].ToString()));
                    SuatChieu.Phong = PhongChieuBUS.GetByID(int.Parse(dr["MaPhong"].ToString()));
                    SuatChieu.GioBD = DateTime.Parse(dr["GioBD"].ToString());
                    SuatChieu.GioKT = DateTime.Parse(dr["GioKT"].ToString());
                    array.Add(SuatChieu);
                }
            }
            return array;
        }

        public int LayLastSuatChieu()
        {
            DataProvider cn = new DataProvider();
            DataTable dt = cn.SqlExcuteQuery("LayLastSuatChieu");
            int _LastID = int.Parse(dt.Rows[0][0].ToString());
            return _LastID;
        }

        public IList GetAllbyMaRap(int _MaRap)
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            SqlParameter param = new SqlParameter("@MaRap", _MaRap);
            DataTable dt = cn.SqlExcuteQuery("LayAllSuatbyMaRap", param);

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    SuatChieuDTO SuatChieu = new SuatChieuDTO();
                    SuatChieu.MaSuatChieu = int.Parse(dr["MaSuatChieu"].ToString());
                    SuatChieu.CaChieu = CaChieuBUS.GetByID(int.Parse(dr["MaCaChieu"].ToString()));
                    SuatChieu.Phong = PhongChieuBUS.GetByID(int.Parse(dr["MaPhong"].ToString()));
                    SuatChieu.GioBD = DateTime.Parse(dr["GioBD"].ToString());
                    SuatChieu.GioKT = DateTime.Parse(dr["GioKT"].ToString());
                    array.Add(SuatChieu);
                }
            }
            return array;
        }



        public void Insert(SuatChieuDTO SuatChieu, int MaCaChieu, int Phong)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@MaSuatChieu", SuatChieu.MaSuatChieu);
            param[1] = new SqlParameter("@MaCaChieu", MaCaChieu);
            param[2] = new SqlParameter("@MaPhong", Phong);
            param[3] = new SqlParameter("@GioBD", SuatChieu.GioBD);
            param[4] = new SqlParameter("@GioKT", SuatChieu.GioKT);
            cn.SqlExcuteNonQuery("InsertSuatChieu", param);
        }

        public void Update(SuatChieuDTO SuatChieu, int MaCaChieu, int Phong)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@MaSuatChieu", SuatChieu.MaSuatChieu);
            param[1] = new SqlParameter("@MaCaChieu", MaCaChieu);
            param[2] = new SqlParameter("@MaPhong", Phong);
            param[3] = new SqlParameter("@GioBD", SuatChieu.GioBD);
            param[4] = new SqlParameter("@GioKT", SuatChieu.GioKT);
            cn.SqlExcuteNonQuery("UpdateSuatChieu", param);
        }

        public void Delete(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaSuatChieu", id);
            cn.SqlExcuteNonQuery("DeleteSuatChieu", param);
        }
    }
}
